﻿/*
 * Created by SharpDevelop.
 * User: Alice
 * Date: 10.03.2012
 * Time: 06:49
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Net;
using System.Collections;
using System.IO;

namespace monoServer
{
	/// <summary>
	/// Description of Configuration.
	/// </summary>
	public class Configuration
	{
		/*
		// General Setup
		public bool debugMode;
		public String serverAliveMessage;
		public int serverAliveMessageCronTime;
		public int serverAliveKeepOutTime;
		public int checkPingTime;
		
		// Threading Setup
		public int threadingPoolSize;
		
		// Chat Setup
		public int loadChatMessageAtStart;
		public int intervalRemoveOldChatMessages;
		public int loadChatMessagesAtLogin;
		
		// Session Setup
		public int sessionKeystartValue;
		public int logoutTimeWithoutPing;
		public int maxAllowedWrongPassword;
		public int bannTimeAtWrongPassword;
		
		// Network Configuration
		public IPAddress localIPAddress;
		public int localPort;
		
		// Database Configuration
		public String databaseServerAddress;
		public User databaseServerUser;
		public String databaseServerName;
		public int databaseConnectionPoolSize;

		// Logger Configuration
		public String logDir;
		public String logDirTelegrams;

		// Admin Configuration
		public IPAddress adminIP;
		public int adminPort;
		public Hashtable adminPermissions;
		
		// Telegram Configuration
		public int maxTelegramSize;
		public int telegramResendTime;
		
		// Misc. Settings
		public ulong telegreamKeyStartValue;
		
		*/
		// ng
		private StreamReader sr;
		private string filename;
		public Hashtable values;
		
		public Configuration()
		{
		}
		
		public bool eof()
		{
			return this.sr.EndOfStream;
		}
		
		public string readLine()
		{
			return this.sr.ReadLine(); 
		}
		
		public Configuration(string file)
		{
			this.values = new Hashtable();
			this.filename = file;
			this.sr = new StreamReader(this.filename);
			this.setup();
		}
		
		public void setup()
		{
			while(!this.sr.EndOfStream)
			{
				string line = this.sr.ReadLine();
				if(!line.StartsWith("#") && !line.Equals(String.Empty))
				{
					string[] segments = line.Split('\t');
					this.values[segments[0].ToLower()] = segments[1];
				}
			}
		}
			// TODO Werte die durch configfile nicht gesetzt werden müssen einen default-Wert bekommen
			/*
			User dbuser = new User();
			this.adminPermissions = new Hashtable();
			StreamReader sr = new StreamReader(file);
			while(!sr.EndOfStream)
			{
				string line = sr.ReadLine();
				if(!line.StartsWith("#") && !line.Equals(string.Empty))
				{
					string[] arr = line.Split('\t');
					string key = arr[0];
					string value = arr[1];
					
					switch(key)
					{
					case "THREADING_POOL_SIZE":
						this.threadingPoolSize = int.Parse(value);
						break;
					case "LOCAL_IP":
						this.localIPAddress = IPAddress.Parse(value);
						break;
					case "LOCAL_PORT":
						this.localPort = int.Parse(value);
						break;
					case "DEBUG_MODE":
						this.debugMode = bool.Parse(value);
						break;
					case "SESSION_KEY_START_VALUE":
						this.sessionKeystartValue = int.Parse(value);
						break;
					case "LOGOUT_TIME_WITHOUT_PING":
						this.logoutTimeWithoutPing = int.Parse(value);
						break;
					case "CHECK_PING_TIME":
						this.checkPingTime = int.Parse(value);
						break;
					case "MAX_ALLOWED_WRONG_PASSWORD":
						this.maxAllowedWrongPassword = int.Parse(value);
						break;
					case "BAN_TIME_AT_WRONG_PASSWORD":
						this.bannTimeAtWrongPassword = int.Parse(value);
						break;
					case "LOAD_CHAT_MESSAGES_AT_START":
						this.loadChatMessageAtStart = int.Parse(value);
						break;
					case "INTERVAL_REMOVE_OLD_CHAT_MESSAGES":
						this.intervalRemoveOldChatMessages = int.Parse(value);
						break;
					case "LOAD_CHAT_MESSAGES_AT_LOGIN":
						this.loadChatMessagesAtLogin = int.Parse(value);
						break;
					case "ALIVE_MESSAGE":
						this.serverAliveMessage = value;
						break;
					case "ALIVE_MESSAGE_CRON_TIME":
						this.serverAliveMessageCronTime = int.Parse(value);
						break;
					case "ALIVE_KEEP_OUT_TIME":
						this.serverAliveKeepOutTime = int.Parse(value);
						break;
					case "DB_IP":
						this.databaseServerAddress = value;
						break;
					case "DB_NAME":
						this.databaseServerName = value;
						break;
					case "DB_USER":
						dbuser.NickName = value;
						break;
					case "DB_PW":
						dbuser.Password = value;
						break;
					case "DB_CON_POOLSIZE":
						this.databaseConnectionPoolSize = int.Parse(value);
						break;
					case "LOGDIR":
						this.logDir = value;
						break;
					case "LOGDIRT":
						this.logDirTelegrams = value;
						break;
					case "ADMIN_IP":
						this.adminIP = IPAddress.Parse(value);
						break;
					case "ADMIN_PORT":
						this.adminPort = int.Parse(value);
						break;
					case "ADMIN_PERMISSION_GETSESSIONS":
						this.adminPermissions.Add("GETSESSIONS", int.Parse(value));
						break;
					case "ADMIN_PERMISSION_LOGOUTUSER":
						this.adminPermissions.Add("LOGOUTUSER", int.Parse(value));
						break;
					case "ADMIN_PERMISSION_SENDTELEGRAMTOSESSION":
						this.adminPermissions.Add("SENDTELEGRAMTOSESSION", int.Parse(value));
						break;
					case "ADMIN_PERMISSION_SETBANTIMEFROMSESSIONKEY":
						this.adminPermissions.Add("SETBANTIMEFROMSESSIONKEY", int.Parse(value));
						break;
					case "TELEGRAM_MAX_SIZE":
						this.maxTelegramSize = int.Parse(value);
						break;
					case "TELEGRAM_RESEND_TIME":
						this.telegramResendTime = int.Parse(value);
						break;
					case "TELEGRAM_KEY_START_VALUE":
						this.telegreamKeyStartValue = ulong.Parse(value);
						break;
					
					// Client Section	
					case "SERVER_IP":
						break;
						
					}
				}
				this.databaseServerUser = dbuser;
				*/
			
		
			/*
			this.debugMode = true;
			this.sessionKeystartValue = 1000;
			this.logoutTimeWithoutPing = 5;
			this.checkPingTime = 4*60;
			this.maxAllowedWrongPassword = 3;
			this.bannTimeAtWrongPassword = 30; // seconds
			
			this.loadChatMessageAtStart = 10; // minutes
			this.intervalRemoveOldChatMessages = 60; // minutes
			this.loadChatMessagesAtLogin = 10; // minutes
			
			this.serverAliveMessage = "Game-Server is running!";
			this.serverAliveMessageCronTime = 10*1;
			this.serverAliveKeepOutTime = 5;
			
			this.localIPAddress = IPAddress.Parse("192.168.0.11");
			this.localPort = 13000;
			
			this.databaseServerAddress = "127.0.0.1";
			this.databaseServerName = "bugserver";
			this.databaseServerUser = new User(0,"bugserver","bugserver");
			this.databaseConnectionPoolSize = 50;
			
			this.logDir = "/var/log/bugserver/";
			this.logDirTelegrams = this.logDir + "telegrams/";
			
			this.adminIP = IPAddress.Parse("127.0.0.1");
			this.adminPort = 13001;
			this.adminPermissions = new Hashtable();
			this.adminPermissions.Add("GETSESSIONS",50);
			this.adminPermissions.Add("LOGOUTUSER",50);
			this.adminPermissions.Add("SENDTELEGRAMTOSESSION",50);
			this.adminPermissions.Add("SETBANNTIMEFROMSESSIONKEY",50);
		
			this.maxTelegramSize = 512;	
			this.telegramResendTime = 5; // in Seunden
			this.telegreamKeyStartValue = 100;
			*/
		
	}
}
